﻿using AutoMapper;
using MachineVision.Domain;
using MachineVision.Service.Dto;
using System.Data;

namespace MachineVision.Service
{
    public class TemperatureService : ITemperatureService
    {
        private readonly VisionDbContext context;
        private readonly IMapper mapper;

        public TemperatureService(VisionDbContext context, IMapper mapper)
        {
            this.mapper = mapper;
            this.context = context;
        }

        public TemperatureRes GetData(int Id)
        {
            var data = context.TemperatureInfos.FirstOrDefault(s => s.Id.Equals(Id));
            return mapper.Map<TemperatureRes>(data);
        }

        public List<TemperatureRes> GetDatas(int pageNum = 1, int pageSize = 20, DateTime? TimeBegin = null, DateTime? TimeEnd = null)
        {
            IEnumerable<TemperatureInfo> data = context.TemperatureInfos;
            if (TimeBegin != null)
                data = data.Where(s => s.TempTime >= TimeBegin);
            if (TimeEnd != null)
                data = data.Where(s => s.TempTime <= TimeEnd);

            data = data.OrderByDescending(s => s.TempTime)
                .Skip((pageNum-1) * pageSize).Take(pageSize);

            //TODO TMP
            var list = data.ToList();
            while (list.Count() < pageSize)
            {
                //data.ToList().ForEach(s=> data.s)
                list.AddRange(data);
                list = list.Take(pageSize).ToList();
            }
            //List<TemperatureRes> list = new List<TemperatureRes>();
            //data.ToList().ForEach(s =>
            //{
            //    list.Add(mapper.Map<TemperatureRes>(s));
            //});
            //return list;

            return mapper.Map<List<TemperatureRes>>(list);
        }

        public List<TemperatureRes> GetDayDatas(int pageNum , int pageSize , DateTime date)
        {
            IEnumerable<TemperatureInfo> data = context.TemperatureInfos.Where(s => s.WorkPlace == 0 || s.WorkPlace == null).Where(s => s.TempTime >= date && s.TempTime< date.AddDays(1));

            data = data.OrderByDescending(s => s.TempTime)
                .Skip((pageNum - 1) * pageSize).Take(pageSize);

            return mapper.Map<List<TemperatureRes>>(data);
        }

        public List<TemperatureRes> Get24Datas(DateTime date,int num)
        {
            IEnumerable<TemperatureInfo> data = context.TemperatureInfos.Where(s => s.TempTime >= date && s.TempTime < date.AddDays(1)).Where(s => s.WorkPlace!=null&& s.WorkPlace != 0).OrderByDescending(s => s.TempTime).Take(num);

            //data = data;
            //    .Skip((pageNum - 1) * pageSize).Take(pageSize);

            return mapper.Map<List<TemperatureRes>>(data);
        }

        public bool AddData(TemperatureReq value)
        {
            TemperatureInfo info = mapper.Map<TemperatureInfo>(value);
            info.CreateTime = DateTime.Now;
            context.TemperatureInfos.Add(info);
                //(new TemperatureInfo {  CreateTime=DateTime.Now, TemperatureC=58, Date= DateOnly.FromDateTime(DateTime.Now), Time = TimeOnly.FromDateTime(DateTime.Now) ,Id= int.MaxValue});
            int a = context.SaveChanges();
            return a>=1 ;
        }


        public bool ModifyData(int Id, TemperatureReq value)
        {
            var data = context.TemperatureInfos.FirstOrDefault(s => s.Id.Equals(Id));
            data.TemperatureC = value.TemperatureC;
            data.Description = value.Description;
            data.Remarks = value.Remarks;
            int a = context.SaveChanges();
            return a > 0;
        }


    }
}